﻿using Yxinmt.Controls;
using Yxinmt.Interface;
using Yxinmt.Utility;
using System.Collections.Generic;

namespace Yxinmt.Components
{
    public class PropertyItem : BaseComponent, IComponent
    {
        IControl _Label { get; set; }
        IControl _Control { get; set; }

        public PropertyItem(Dictionary<string, object> property, IWindow form)
        {
            _Property = property;
            Window = form;
            this.InitSet();

            this.ControlList = new List<IControl>();
            ComponentList = new List<IComponent>();

            string controlType = _Property.GetStringValue("type");

            this.SetDefaultValue(controlType);

            //if (controlType != "Label")
            //{
            //    string label = _Property.GetStringValue("Label");
            //    if (!string.IsNullOrEmpty(label))
            //    {
            //        property = new Dictionary<string, object>();
            //        foreach (var kvp in _Property)
            //        {
            //            property[kvp.Key] = kvp.Value;
            //        }

            //        if (property.ContainsKey("LabelX")) property["X"] = property["LabelX"];
            //        if (property.ContainsKey("LabelY")) property["Y"] = property["LabelY"];
            //        if (property.ContainsKey("LabelPadding")) property["Padding"] = property["LabelPadding"];

            //        this._Label = new Label(property, this);
            //        this.ControlList.Add(_Label);

            //        if (_Property.ContainsKey("ControlTextAlign")) _Property["TextAlign"] = _Property["ControlTextAlign"];
            //    }
            //    else  if (!_Property.ContainsKey("IsLabel")) _Property["IsLabel"] = false;
            //}

            GetRightControl(controlType);
        }

        private void GetRightControl(string type)
        {
            switch (type)
            {
                case "Button": _Control = new Button(_Property, this); break;
                case "Label": _Control = new Label(_Property, this); break;
                case "Image": _Control = new Image(_Property, this); break;
                case "PopupImage": _Control = new PopupImage(_Property, this); break;
                case "TextBlock": _Control = new TextBlock(_Property, this); break;
                case "TextBox": _Control = new TextBox(_Property, this); break;
                case "ComboBox": _Control = new ComboBox(_Property, this); break;
                case "WebView2": _Control = new WebView2(_Property, this); break;
                case "ImageSelectButton": _Control = new ImageSelectButton(_Property, this); break;
                case "ImageTextButton": _Control = new ImageTextButton(_Property, this); break;
            }

            if (_Control != null) this.ControlList.Add(this._Control);
        }

        private void SetDefaultValue(string controlType)
        {
            SetDefaultValue("IsDefaultValue", false);

            bool blDefaultValue = !_Property.ContainsKey("IsDefaultValue") ? true : _Property.GetValue<bool>("IsDefaultValue");

            if (blDefaultValue)
            {
                this.SetDefaultValue("Width", 310);
                this.SetDefaultValue("Height", 40);
                this.SetDefaultValue("LabelWidth", 100);

                if (controlType.Equals("Button")) this.SetDefaultValue("ControlWidth", 60);
                else this.SetDefaultValue("ControlWidth", 200);
            }
        }

        private void SetDefaultValue(string name, object value)
        {
            if (!this._Property.ContainsKey(name)) this._Property[name] = value;
        }

        public override object GetValue()
        {
            return this.ControlList[this.ControlList.Count - 1].GetValue();
        }

        public override void SetValue(object value)
        {
            this.ControlList[this.ControlList.Count - 1].SetValue(value);
        }

        public override bool ValidateNullable(out string message)
        {
            message = string.Empty;
            return this.ControlList[this.ControlList.Count - 1].ValidateNullable(out message);
        }
    }
}
